package com.feicheng.iou.service.impl.weChat;

import com.feicheng.iou.pojo.WxPayNotify;
import com.feicheng.iou.service.student.open.StudentOpenService;
import com.feicheng.iou.service.weChatService.WeChatPayNotifyService;
import com.feicheng.iou.weChatSdk.WXPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WeChatPayNotifyServiceImpl implements WeChatPayNotifyService {
    @Autowired
    StudentOpenService studentOpenService;

    /**
     * @param param  微信原样返回的同一下单参数,额外返回交易号等信息
     * @return
     * @description 支付结果通知回调,将已付款的学生的分期支付状态改为已支付,待确认.
     * @throws Exception
     */
    @Override
    public String success(WxPayNotify param) throws Exception {
        log.info("WeChatPayNotifyServiceImpl的微信支付结果通知方法success,参数param为"+param);
        log.info("openid为"+param.getOpenid());
        log.info("transaction_id为"+param.getTransaction_id());
        log.info("total_fee"+param.getTotal_fee());
        log.info("attach"+param.getAttach());
        //PaymentItemId
        Integer paymentItemId = Integer.parseInt(param.getAttach());

        Map<String,String> result = new HashMap<>();
        if("SUCCESS".equals(param.getReturn_code())){
            result.put("return_code", "SUCCESS");
            result.put("return_msg","OK");
            studentOpenService.repayment(paymentItemId,param.getTransaction_id());
        }
        return WXPayUtil.mapToXml(result);

    }
}
